local rule = fk.CreateSkill {
  name = "#hx__skillchanging_rule&",
}

Fk:loadTranslationTable{
  ["@hx__skillchanging_mark-turn"] = "神乎其技",
  ["#hx__skillchanging_rule&"] = "神乎其技",
  ["@[:]hx__skillchanging_negative"] = "妒技",
}

--黑名单
local black_package_name = {}
local black_package_extensionName = {"variation_hegemony","hegemony",}

--神乎其技
rule:addEffect(fk.AfterSkillEffect, {
  can_refresh = function (self, event, target, player, data)
    return target == player and player:hasSkill(data.skill.name) and data.skill:isPlayerSkill(target) and player.room and data.skill.visible
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local allSkills = {}
    local getskills = {}
    local Generals
    if room:getTag("hx__skillchanging_all") == true then
      Generals= Fk.generals
    else
      Generals = Fk:getAllGenerals()
    end
    for _, general in pairs(Generals) do
      if not general.hidden and not general.total_hidden then
        for _, skillName in ipairs(general:getSkillNameList()) do
          local s = Fk.skills[skillName]
          if not skillName:startsWith("#") and not skillName:endsWith("&") and skillName ~= data.skill.name and player.general ~= "mouxusheng"
          and not s:isEquipmentSkill() and not s.cardSkill and s:isPlayerSkill() and not table.contains(black_package_name, s.package)
          and not table.contains(black_package_extensionName, s.package.extensionName)
          then
            table.insert(allSkills, skillName)
          end
        end
      end
    end
    if #allSkills > 0 then
      local char = {}
      local trans = Fk:translate(data.skill.name)
      for _, c in utf8.codes(trans) do
        char[utf8.char(c)] = true
      end
      for _, skillName in ipairs(allSkills) do
        local trans2 = Fk:translate(skillName)
        for _, c in utf8.codes(trans2) do
          local element = utf8.char(c)
          if char[element] then
            table.insert(getskills, skillName)
            break
          end
        end
      end
      room:handleAddLoseSkills(player, "-" .. data.skill.name)
      local description = Fk:getDescription(data.skill.name, "zh_CN")
      room:sendLog{ type = "" .. description .. "", }
      room:addPlayerMark(player, "@hx__skillchanging_mark-turn", 1)
      -- if #player:getSkillNameList() < 20 then
      --   room:addPlayerMark(player, "@hx__skillchanging_mark-turn", 1)
      -- else
      --   local x = math.max(1, #player:getSkillNameList()-20)
      --   room:addPlayerMark(player, "@hx__skillchanging_mark-turn", x)
      -- end
      local a = player:getMark("@hx__skillchanging_mark-turn")
      if a % 5 == 0 or (a % 2 == 0 and a > 5) then
        room:loseHp(player, 1)
        local cards = player:getCardIds{Player.Hand, Player.Equip}
        if #cards > 0 and not player.dead then
          local card = cards[math.random(1, #cards)]
          room:moveCardTo(card, Card.DiscardPile, nil,fk.ReasonDiscard, self.name, nil, true, player)
        end
      end
      if a % 10 == 0 then
        room:changeMaxHp(player, -1)
      end
      if a > 30 then
        room:doBroadcastNotify("ShowToast", "神乎其技")
        local mark = {}
        for _, skill in pairs(player.player_skills) do
          if skill:isPlayerSkill(player) and skill.visible then
            table.insertIfNeed(mark, skill.name)
          end
        end
        for _, skill_name in ipairs(mark) do
          room:handleAddLoseSkills(player, "-" .. skill_name)
        end
        if player.deputyGeneral == nil then
          room:doBroadcastNotify("ShowToast", "" .. Fk:translate(player.general) .. "已被封魔所有技能持续一个阶段")
        else
          room:doBroadcastNotify("ShowToast", "" .. Fk:translate(player.general) .. "/" .. Fk:translate(player.deputyGeneral) .. "已被封魔所有技能持续一个阶段")
        end
        player.tag["hx__skillchanging_mark"] = mark
        room:setPlayerMark(player, "@!hx__skillchanging-phase", 1)
      end
      if #getskills > 0 and #player:getSkillNameList() < 20 then
        local toget = table.random(getskills)
        room:handleAddLoseSkills(player, toget)
      else
        local toget = table.random(allSkills)
        room:handleAddLoseSkills(player, toget)
      end
    else
      room:doBroadcastNotify("ShowToast", "谋徐盛")
    end
  end
})

--清除30标记的封魔
rule:addEffect(fk.EventPhaseEnd, {
  mute = true,
  can_refresh = function (self, event, target, player, data)
    return true
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    for _, p in pairs(room.alive_players) do
      if p.tag["hx__skillchanging_mark"] ~= nil and #p.tag["hx__skillchanging_mark"] > 0 then
        local mark = p.tag["hx__skillchanging_mark"]
        for _, skill_name in ipairs(mark) do
          room:handleAddLoseSkills(p, skill_name)
        end
        p.tag["hx__skillchanging_mark"] = nil
        room:setPlayerMark(p, "@!hx__skillchanging-phase", 0)
      end
    end
  end
})

--黑名单
local generalBlackList = {
  "zuoci", "ol_ex__zuoci", "js__xushao", "shichangshi", "starsp__xiahoudun"
}

--受到伤害切换主将
rule:addEffect(fk.Damaged, {
  can_refresh = function (self, event, target, player, data)
    return target == player and player:isAlive()
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local generals = table.filter(Fk:getAllGenerals(), function (general)
      return not table.contains(generalBlackList, general.name) and not table.contains(black_package_extensionName, general.package.extensionName)
      and not general.hidden and not general.total_hidden
    end)
    if #generals > 0 then
      room:returnToGeneralPile({ player.general })
      room:changeHero(player, table.random(generals).name, false, false, true, false)
    end
  end,
})

--排名播报
rule:addEffect(fk.Deathed, {
  can_refresh = function (self, event, target, player, data)
    return target == player
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local rank = #room.alive_players + 1
    room:sendLog{ type = "" ..Fk:translate(player.general).. "获得第"..rank.."名", toast = true }
  end,
})

--首轮之后每轮开始时随机分配座位
rule:addEffect(fk.RoundStart, {
  priority = 0.001,
  mute = true,
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    return player.seat == 1
  end,
  on_trigger = function (self, event, target, player, data)
    local room = player.room
    local players = room.players
    if room:getBanner("RoundCount") <= 1 then return false end
    local player_circle = {}
    local player_name = {}
    local p = 1
    local x = math.random(1,#players)
    local one = players[x]  --随机的1号位
    for i = 1, #players do
      if players[i] == one then
        p = i
        break
      end
    end
    for j = p, #players do
      table.insert(player_circle, players[j])
      local pname = players[j]._splayer:getScreenName()
      table.insert(player_name, pname)
      local num = j-p +1
    end
    for j = 1, p - 1 do
      table.insert(player_circle, players[j])
      local pname = players[j]._splayer:getScreenName()
      table.insert(player_name, pname)
      local num = j +3
    end
    local banner = room:getBanner("hx__skillchanging_player_circle")
    if not banner then
      room:delay(2000)
      room:arrangeSeats(player_circle)
      for i = 1, #player_name do
        player_name[i] = i.."号位："..player_name[i]
      end
      room:sendLog{ type = "当前座位顺序是：<br> %arg", arg = table.concat(player_name, "<br>"), toast = true }
      room:setBanner("hx__skillchanging_player_circle", table.concat(player_name, "、"))
    end
  end,
})

--只是上面的内容会调两次。。
rule:addEffect(fk.RoundEnd, {
  priority = 0.001,
  mute = true,
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    return player.seat == 1
  end,
  on_trigger = function (self, event, target, player, data)
    local room = player.room
    room:setBanner("hx__skillchanging_player_circle", nil)
  end,
})

-- --每回合开始随机刷新全局负面
-- rule:addEffect(fk.TurnStart, {
--   mute = true,
--   can_refresh = function (self, event, target, player, data)
--     return target == player
--   end,
--   on_refresh = function (self, event, target, player, data)
--     local room = player.room
--     local index = math.random(1, 7)
--     local negative = {
--       "skillchange_discard",  --弃牌
--       "skillchange_losehp",  --失去体力
--     }
--     room:setBanner("@[:]hx__skillchanging_negative", negative[index])
--   end,
-- })

--获胜
rule:addEffect(fk.GameOverJudge, {
  can_refresh = function (self, event, target, player, data)
    return target == player
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    room:setTag("SkipGameRule", true)
    if #room.alive_players == 1 then
      local winner = Fk.game_modes[room.settings.gameMode]:getWinner(player)
      if winner ~= "" then
        room:gameOver(winner)
      end
    end
  end,
})



return rule
